package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.commons.utils.UniqueSixDigitGenerator;
import com.example.config.ResponseVO;
import com.example.pojo.EnrollInfo;
import com.example.pojo.TeacherInfo;
import com.example.service.TeacherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherInfoController {
    @Resource
    private TeacherInfoService teacherInfoService;

    @PostMapping("/teacherInfoList")
    public ResponseVO<List<TeacherInfo>> teacherListResponseVO() {

        List<TeacherInfo> teacherInfos = teacherInfoService.teacherInfoList();
        return ResponseVO.success(teacherInfos);
    }

    @PostMapping("/teacherInfoListPage/{currentPage}/{limit}")
    public ResponseVO<IPage<TeacherInfo>> teacherInfoListPage(@PathVariable int currentPage, @PathVariable int limit) {
        IPage<TeacherInfo> iPage = teacherInfoService.teacherInfoListPage(currentPage, limit);
        return ResponseVO.success(iPage);
    }

    @PostMapping("/teacherInfoListPage/{currentPage}/{limit}/{teacherName}")
    public ResponseEntity<ResponseVO<IPage<TeacherInfo>>> getTeacherInfoPage(@PathVariable int currentPage, @PathVariable int limit, @PathVariable String teacherName) {
        IPage<TeacherInfo> teacherInfoIPage = teacherInfoService.getTeacherByName(currentPage, limit, teacherName);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "教师信息获取成功", teacherInfoIPage));
    }

    @PostMapping("/addTeacher")
    public ResponseEntity<ResponseVO<String>> addTeacher(@RequestBody TeacherInfo teacherInfo) {
        UniqueSixDigitGenerator generator = new UniqueSixDigitGenerator();
        teacherInfo.setBindCode(generator.generateUniqueSixDigitString());
        int i = teacherInfoService.addTeacher(teacherInfo);
        if (i > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "教师添加成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }

    @PostMapping("/updateTeacher")
    public ResponseEntity<ResponseVO<String>> updateTeacher(@RequestBody TeacherInfo teacherInfo) {
        int i = teacherInfoService.updateTeacher(teacherInfo);
        if (i > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "教师修改成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }

    @DeleteMapping("/batchDeleteTeacherInfo")
    public ResponseEntity<ResponseVO<String>> batchDeleteTeacherInfo(@RequestParam List<Long> teacherInfoArr) {
        System.out.println(teacherInfoArr);
        try {
            int result = teacherInfoService.batchDeleteTeacherInfo(teacherInfoArr);
            if (result > 0) {
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "删除成功"));
            } else {
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "删除教师失败,请稍后再试"));
            }
        } catch (Exception e) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "删除教师失败 " + e.getMessage()));
        }

    }

    @DeleteMapping("/deleteTeacherInfoById/{deleteId}")
    public ResponseEntity<ResponseVO<String>> deleteTeacherInfo(@PathVariable("deleteId") Long deleteId) {
        int resultRow = teacherInfoService.deleteTeacherInfo(deleteId);
        if (resultRow > 0) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "教师删除成功", "success"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.NOT_FOUND, "系统繁忙，请稍后再试", null));
    }
}
